﻿#ifndef MEGA16_SOS_H_
#define MEGA16_SOS_H_

#include<avr/io.h>
#include<avr/Interrupt.h>


//防止被编译器占用
register unsigned char tempR2 asm("r2");
register unsigned char tempR3 asm("r3");
register unsigned char tempR4 asm("r4");
register unsigned char tempR5 asm("r5");
register unsigned char tempR6 asm("r6");
register unsigned char tempR7 asm("r7");
register unsigned char tempR8 asm("r8");
register unsigned char tempR9 asm("r9");
register unsigned char tempR10 asm("r10");
register unsigned char tempR11 asm("r11");
register unsigned char tempR12 asm("r12");
register unsigned char tempR13 asm("r13");
register unsigned char tempR14 asm("r14");
register unsigned char tempR15 asm("r15");
register unsigned char tempR16 asm("r16");
register unsigned char tempR17 asm("r17");


#define OS_START_SP_INIT(sp,taskStackTop) {            \
	sp = taskStackTop + 19;  \
	__asm__ __volatile__( "reti" " \t" ); }


#define STACK_PUSH_OP(sp)     {                     \
	sp = sp + 3;                                       \
	__asm__ __volatile__("PUSH __zero_reg__ \t");      \
	__asm__ __volatile__("PUSH __tmp_reg__ \t");       \
	__asm__ __volatile__("IN __tmp_reg__,__SREG__ \t");\
	__asm__ __volatile__("PUSH __tmp_reg__ \t");       \
	__asm__ __volatile__("CLR __zero_reg__ \t");       \
	__asm__ __volatile__("PUSH R18 \t");               \
	__asm__ __volatile__("PUSH R19 \t");               \
	__asm__ __volatile__("PUSH R20 \t");               \
	__asm__ __volatile__("PUSH R21 \t");               \
	__asm__ __volatile__("PUSH R22 \t");               \
	__asm__ __volatile__("PUSH R23 \t");               \
	__asm__ __volatile__("PUSH R24 \t");               \
	__asm__ __volatile__("PUSH R25 \t");               \
	__asm__ __volatile__("PUSH R26 \t");               \
	__asm__ __volatile__("PUSH R27 \t");               \
	__asm__ __volatile__("PUSH R30 \t");               \
	__asm__ __volatile__("PUSH R31 \t");               \
	__asm__ __volatile__("PUSH R28 \t");               \
	__asm__ __volatile__("PUSH R29 \t");			   \
/*	tempR2 = DDRA;*/									   \
/*	__asm__ __volatile__("PUSH R2 \t");*/				   \
/*	tempR3 = PORTA;						*/			   \
/*	__asm__ __volatile__("PUSH R3 \t");	*/			   \
	tempR4 = DDRB;									   \
	__asm__ __volatile__("PUSH R4 \t");				   \
	tempR5 = PINB;									   \
	__asm__ __volatile__("PUSH R5 \t");	      }

/*	__asm__ __volatile__("POP R5 \t");					 \
    PORTB = tempR5;										 \
	__asm__ __volatile__("POP R4 \t");					 \
    DDRB = tempR4;										 \
	__asm__ __volatile__("POP R3 \t");					 \
    PORTA = tempR3;										 \
	__asm__ __volatile__("POP R2 \t");					 \
    DDRA = tempR2;										 \*/

#define STACK_POP_OP()         {        	             \
	__asm__ __volatile__("POP R5 \t");					 \
    PORTB = tempR5;										 \
	__asm__ __volatile__("POP R4 \t");					 \
    DDRB = tempR4;										 \
	/*__asm__ __volatile__("POP R3 \t");*/					 \
    /*PORTA = tempR3;						*/				 \
	/*__asm__ __volatile__("POP R2 \t");*/					 \
    /*DDRA = tempR2;*/										 \
	__asm__ __volatile__("POP R29 \t");                  \
	__asm__ __volatile__("POP R28 \t");                  \
	__asm__ __volatile__("POP R31 \t");                  \
	__asm__ __volatile__("POP R30 \t");                  \
	__asm__ __volatile__("POP R27 \t");                  \
	__asm__ __volatile__("POP R26 \t");                  \
	__asm__ __volatile__("POP R25 \t");                  \
	__asm__ __volatile__("POP R24 \t");                  \
	__asm__ __volatile__("POP R23 \t");                  \
	__asm__ __volatile__("POP R22 \t");                  \
	__asm__ __volatile__("POP R21 \t");                  \
	__asm__ __volatile__("POP R20 \t");                  \
	__asm__ __volatile__("POP R19 \t");                  \
	__asm__ __volatile__("POP R18 \t");                  \
	__asm__ __volatile__("POP __tmp_reg__ \t");          \
	__asm__ __volatile__("OUT __SREG__,__tmp_reg__ \t"); \
	__asm__ __volatile__("POP __tmp_reg__ \t");          \
	__asm__ __volatile__("POP __zero_reg__ \t");         \
	__asm__ __volatile__("ret");      }       
	
#define CHANGE_SP_TO_RUNNING_TASK(sp,runningTaskStackTop) {               \
    cli();                                        \
	sp = runningTaskStackTop;   \
	sei();   }   
	
#define SOS_TIMER_INTERRUPT SIGNAL(SIG_OVERFLOW0)  

#define SOS_TIMER_CLEAR(timerInitValue) TCNT0 = timerInitValue;   

#define SOS_TIMER_INIT(timerInitValue) {									\
	TCCR0 = 0;										\
	TCCR0 |= (1<<CS02); 		/* 256预分频 */		\
	TIMSK |= (1<<TOIE0); 		/* T0溢出中断允许 */\
	TCNT0 = timerInitValue;				}	/* 置计数起始值 */	


#define SOS_TASK_STACK_INIT(taskName,taskId,tcbTaskStackTop,manualStack)  {															\
	unsigned char i;																				\
	unsigned char *localTaskStack = &manualStack[taskId * 50 + 49];											\
	*localTaskStack-- = (unsigned int)taskName;    /*将任务的地址低位压入堆栈*/								\
	*localTaskStack-- = (unsigned int)taskName>>8; /*将任务的地址高位压入堆栈*/								\
	*localTaskStack-- = 0x00; /*R1 __zero_reg__*/															\
	*localTaskStack-- = 0x00; /*R0 __tmp_reg__*/															\
	*localTaskStack-- = 0x80; /*SREG 在任务中，开启全局中断*/												\
	for(i = 0; i < 14; i++)																				\
	{																								\
		*localTaskStack--=i;																				\
	}																								\
	*localTaskStack-- = 0x00;/*初始化PORTB*/																\
	*localTaskStack-- = 0x00;/*初始化PORTB*/																\
	/**taskStack-- = 0x00;初始化PORTB*/																\
	/**taskStack-- = 0x00;初始化PORTB*/																\
	tcbTaskStackTop = (unsigned int)localTaskStack; /*将人工堆栈的栈顶，保存到堆栈的数组中*/	\
}

#define TASK_STACK_SIZE(sosSupportTaskNum) sosSupportTaskNum * 50

#endif
